/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.dialogs.TabbedDialog;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.materialMedia.material.MSpring;
import br.ufmg.dees.insane.materialMedia.material.Material;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.dialogs.ElmElasticConnectionsDialog;

/**
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since April 2004
 */
public class SetElmElasticConnectionsCommand implements Command, Cloneable {
	
	private InsaneInternalInterface parent;
	private Controller controller;
	private DrawingArea da;
	
//*****************************************************************************
	
	/** Creates new SetElmElasticConnectionsCommand */
	public SetElmElasticConnectionsCommand(JInternalFrame parent) {
		this.parent = (InsaneInternalInterface)parent;
		controller = this.parent.getController();
		da = this.parent.getDrawing();
	}
	
//*****************************************************************************
	
	/** Execute method. */
	public void execute() {
		
		FemModel fmodel = parent.getModel().getDiscreteModel();
		
		boolean changed = false;
		
		ArrayList elms = controller.getSelection();
		if ( elms.size() < 1 ) {
			controller.selectAll();
			elms = controller.getSelection();
		}
		
		if (elms.size() == 0) {
			parent.addOutputText("There is no elements in model.");
			return;
		}
		
        ElmElasticConnectionsDialog dlg = new ElmElasticConnectionsDialog(parent);
        int retVal = dlg.showDialog();
        if (retVal == TabbedDialog.OK_OPTION){
			
			ListIterator el = elms.listIterator();
			while(el.hasNext()) {
				FrameElement elm = (FrameElement)el.next();
				
				//Elastic connection at initial node
				if (dlg.K1dx!=0 || dlg.K1dy!=0 || dlg.K1rz!=0) {
					//There is a elastic connection
					
					int numMat = 0;
					MSpring mat2 = new MSpring();
					mat2.setLabel("empty");
					
					ListIterator mats = fmodel.getMaterialsList().listIterator();
					while (mats.hasNext()) {
						Material mat1 = (Material) mats.next();
						if (mat1 instanceof MSpring) {
							numMat++;
							mat1 = (MSpring)mat1;
							if (((MSpring)mat1).getStiffness(0)==dlg.K1dx && ((MSpring)mat1).getStiffness(1)==dlg.K1dy && ((MSpring)mat1).getStiffness(5)==dlg.K1rz) {
								mat2 = (MSpring)mat1;
							}
						}
					}
					
					if (mat2.getLabel().equals("empty")) {
						mat2 = new MSpring();
						String str = "MSpring" + String.valueOf(numMat+1);
						mat2.setLabel(str);
						mat2.setStiffness(0, dlg.K1dx);
						mat2.setStiffness(1, dlg.K1dy);
						mat2.setStiffness(2, dlg.K1dz);
						mat2.setStiffness(3, dlg.K1rx);
						mat2.setStiffness(4, dlg.K1ry);
						mat2.setStiffness(5, dlg.K1rz);
						fmodel.add(mat2);
					}
					
					ListIterator elms2 = fmodel.getElementsList().listIterator();
					while (elms2.hasNext()) {
						FrameElement elm2 = (FrameElement) elms2.next();
						Node elm2fn = (Node)elm2.getIncidence().get(1);
						Node elm1in = (Node)elm.getIncidence().get(0);
						if (elm2fn==elm1in) {/*
							if (elm2.getMaterial() instanceof MSpring)){
								elm2.setMaterial(mat2);
							} else {
								
								FrameElement newelm = new FrameElement()
								//newelm.add();
								*/
								
							}
						}
					} else {
					//There in no elastic connection
					
				}
				
				//Elastic connection at final node
				
			}
			
		}
		
		//parent.addToUndo((Command)this.clone());
		controller.unSelectAll();
		controller.compose();
		da.repaint();
		
		if (changed)
			parent.cleanResults();
		
	}
	
//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
//*****************************************************************************
	
	/** Clone method. */
	public Object clone() {
		try {
			SetElmElasticConnectionsCommand cmd = (SetElmElasticConnectionsCommand)super.clone();
			return cmd;
		}catch (CloneNotSupportedException e) {
			throw new Error("Clonning ERROR - This should never happen!");
		}
	}
	
//*****************************************************************************
}
